<?php
/*
 * 2021/7/16
 * 20:03
 * User: 2650332702@qq.com
 */

namespace App\Api\Controllers;


use App\Api\Manages\CommonController;
use App\Api\Repositories\GroupingRepositorie;
use App\Api\Services\JsonService;
use Illuminate\Http\Request;

class GroupingController  extends CommonController
{
    //查找是否存在拼团
    public function havePintuan(Request $request)
    {
        //获取用户ID
        $validat = $request->validate([
            'uid' => ['required'],
        ]);
        $uid = (int)$validat['uid'];
        $grouping = GroupingRepositorie::haveExistence($uid);
        if(!$grouping){
            return JsonService::fail('暂无拼团活动参加','',config('code.HTTP_NULL_DATA'));
        }else{
            return JsonService::success('用户存在拼团',$grouping,config('code.HTTP_OK'));
        }
    }

    //拼团列表
    public function groupList()
    {
        $time = $this->NowTime();
        $goodlist = GroupingRepositorie::groupList($time);
        if($goodlist){
            return JsonService::success('查询成功',$goodlist,config('code.HTTP_OK'));
        }else{
            return JsonService::fail('暂无拼团活动参加','',config('code.HTTP_NULL_DATA'));
        }
    }

    //拼团详情
    public function groupDetail(Request $request)
    {
        //获取拼团ID
        $validat = $request->validate([
            'pid' => ['required'],
        ]);
        $group = GroupingRepositorie::groupDetail((int)$validat['pid']);
        if(empty($group)){
            return JsonService::fail('获取数据失败','',config('code.HTTP_NULL_DATA'));
        }else{
            return JsonService::success('查询成功',$group,config('code.HTTP_OK'));
        }
    }
    //拼团组详情
    public function groupAll(Request $request)
    {
        //获取拼团ID
        $validat = $request->validate([
            'pid' => ['required'],
        ]);
        //拼团组详情
        $allow_field = [
            'head_member_img',
            'pintuan_num',
            'pintuan_count'
        ];
        $grouplist = GroupingRepositorie::getAllGroup($validat['pid'],$allow_field);
        if(empty($grouplist)){
            return JsonService::fail('获取数据失败','',config('code.HTTP_NULL_DATA'));
        }else{
            return JsonService::success('查询成功',$grouplist,config('code.HTTP_OK'));
        }
    }
    //立即拼团
    public function nowPintuan(Request $request)
    {
        //获取用户ID 拼团活动ID
        $validat = $request->validate([
            'uid' => ['required'],
            'pid' => ['required'],
            'addr_id' => ['required'],
        ]);
        //判断用户是否已经拼团
        $count = GroupingRepositorie::is_pintuan((int)$validat['uid'],(int)$validat['pid']);
        if($count){
            //查询用户地址
            $addr = GroupingRepositorie::getAddr((int)$validat['addr_id']);
            return JsonService::success('确认收货地址',$addr,1004);
        }
        $allow_field = [
            'site_id',
            'goods_id',
            'is_virtual_goods',
            'pintuan_num',
            'end_time',
            'is_virtual_buy',
            'is_single_buy',
            'is_promotion',
            'buy_num',//拼团限制购买
            'pintuan_id'
        ];
        //根据拼团ID查询
        $nowpintuan = GroupingRepositorie::joinPintuan((int)$validat['uid'],$allow_field,(int)$validat['addr_id'],(int)$validat['pid']);
         if($nowpintuan){
             return JsonService::success('拼团成功','',1000);
         }else{
             return JsonService::fail('拼团失败','',1003);
         }
    }
}
